package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @FileName ListValueConstraintValidator
 * @Description 自定义的注解校验器
 * @Author msh
 * @date 2025-10-18
 **/

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    private Set<Integer> set = new HashSet<>();

    @Override
    //初始化方法
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for(int val : vals){
            set.add(val);
        }
    }

    /**
     *
     * @param value 待校验的值
     * @param context 校验上下文
     * @return 校验结果
     */
    @Override
    //判断是否校验成功
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        return set.contains(value);
    }
}
